package com.zdkj.subway.support.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.support.entity.SwStTicketInformationCheck;
import com.zdkj.subway.support.service.SwStTicketInformationCheckService;
import com.zdkj.subway.support.util.LocalDateConverter;
import com.zdkj.subway.support.util.LocalDateTimeConverter;
import com.zdkj.subway.support.util.UploadDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


/**
 * ACC操作人员回馈车站提交票卡信息
 *
 * @author cuilei
 * @date 2020-05-13 16:53:16
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swstticketinformationcheck")
@Api(value = "swstticketinformationcheck", tags = "ACC操作人员回馈车站提交票卡信息")
public class SwStTicketInformationCheckController {

    private final SwStTicketInformationCheckService swStTicketInformationCheckService;

    /**
     * 分页查询
     *
     * @param page                       分页对象
     * @param swStTicketInformationCheck ACC操作人员回馈车站提交票卡信息
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwStTicketInformationCheckPage(Page page, SwStTicketInformationCheck swStTicketInformationCheck) {
        return R.ok(swStTicketInformationCheckService.page(page, Wrappers.query(swStTicketInformationCheck).lambda().
                in(SwStTicketInformationCheck::getAccDataStatus, 1, 2)));
    }


    /**
     * 通过id查询ACC操作人员回馈车站提交票卡信息
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swStTicketInformationCheckService.getById(id));
    }

    /**
     * 新增ACC操作人员回馈车站提交票卡信息
     *
     * @param swStTicketInformationCheck ACC操作人员回馈车站提交票卡信息
     * @return R
     */
    @ApiOperation(value = "新增ACC操作人员回馈车站提交票卡信息", notes = "新增ACC操作人员回馈车站提交票卡信息")
    @SysLog("新增ACC操作人员回馈车站提交票卡信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_st_ticket_information_check_add')")
    public R save(@RequestBody SwStTicketInformationCheck swStTicketInformationCheck) {
        return R.ok(swStTicketInformationCheckService.save(swStTicketInformationCheck));
    }

    /**
     * 修改ACC操作人员回馈车站提交票卡信息
     *
     * @param swStTicketInformationCheck ACC操作人员回馈车站提交票卡信息
     * @return R
     */
    @ApiOperation(value = "修改ACC操作人员回馈车站提交票卡信息", notes = "修改ACC操作人员回馈车站提交票卡信息")
    @SysLog("修改ACC操作人员回馈车站提交票卡信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_st_ticket_information_check_edit')")
    public R updateById(@RequestBody SwStTicketInformationCheck swStTicketInformationCheck) {
        return R.ok(swStTicketInformationCheckService.updateById(swStTicketInformationCheck));
    }

    /**
     * 通过id删除ACC操作人员回馈车站提交票卡信息
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除ACC操作人员回馈车站提交票卡信息", notes = "通过id删除ACC操作人员回馈车站提交票卡信息")
    @SysLog("通过id删除ACC操作人员回馈车站提交票卡信息")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_sw_st_ticket_information_check_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swStTicketInformationCheckService.removeById(id));
    }

    /**
     * 下发查询信息到线网
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "下发查询信息到线网", notes = "下发查询信息到线网")
    @SysLog("下发查询信息到线网")
    @PostMapping("/publish")
//    @PreAuthorize("@pms.hasPermission('support_sw_st_ticket_information_check_publish')")
    public R publish(@RequestBody List<Integer> ids) {
        List<SwStTicketInformationCheck> publishList = new ArrayList<>();
        for (Integer id : ids) {
            SwStTicketInformationCheck swStTicketInformationCheck = swStTicketInformationCheckService.getById(id);
            //业务不明确，暂时屏蔽
//            if (swStTicketInformationCheck.getLineDataStatus() == 2 && swStTicketInformationCheck.getAccDataStatus() == 1) {
                swStTicketInformationCheck.setLineDataStatus(3);
                swStTicketInformationCheck.setAccDataStatus(2);
//            }
            publishList.add(swStTicketInformationCheck);
        }
        return R.ok(swStTicketInformationCheckService.updateBatchById(publishList));
    }


    /**
     * 导入回馈车站提交票卡信息
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入回馈车站提交票卡信息", notes = "导入回馈车站提交票卡信息")
    @SysLog("导入回馈车站提交票卡信息")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SwStTicketInformationCheck.class,
                    new UploadDataListener(swStTicketInformationCheckService, SwStTicketInformationCheck.class)).sheet().doRead();
            return R.ok("导入成功");
        } catch (IOException e) {
            log.info(e.getMessage());
            return R.ok("导入失败");
        }
    }

    /**
     * 导出回馈车站提交票卡信息
     *
     * @param swStTicketInformationCheck
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "导出回馈车站提交票卡信息", notes = "导出回馈车站提交票卡信息")
    @GetMapping("/export")
    @SysLog("导出回馈车站提交票卡信息")
    public void exportExcel(SwStTicketInformationCheck swStTicketInformationCheck, HttpServletResponse response) throws IOException {
        List<SwStTicketInformationCheck> swStTicketHandList = swStTicketInformationCheckService.list(Wrappers.query(swStTicketInformationCheck));
        String fileName = URLEncoder.encode("回馈车站提交票卡信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SwStTicketInformationCheck.class).registerConverter(new LocalDateTimeConverter())
                .registerConverter(new LocalDateConverter()).sheet("回馈车站提交票卡信息").doWrite(swStTicketHandList);
    }
}
